package com.zhonghui.mes.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.zhonghui.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhonghui.mes.domain.MesBomDetailItem;
import com.zhonghui.mes.mapper.MesBomMapper;
import com.zhonghui.mes.domain.vo.MesBomVo;
import com.zhonghui.mes.service.IMesBomService;

/**
 * 制造执行MES-BOMService业务层处理
 * 
 * @author qiezi
 * @date 2023-12-25
 */
@Service
public class MesBomServiceImpl implements IMesBomService 
{
    @Autowired
    private MesBomMapper mesBomMapper;

    /**
     * 查询制造执行MES-BOM
     * 
     * @param id 制造执行MES-BOM主键
     * @return 制造执行MES-BOM
     */
    @Override
    public MesBomVo selectMesBomById(Long id)
    {
        return mesBomMapper.selectMesBomById(id);
    }

    /**
     * 查询制造执行MES-BOM列表
     * 
     * @param mesBom 制造执行MES-BOM
     * @return 制造执行MES-BOM
     */
    @Override
    public List<MesBomVo> selectMesBomList(MesBomVo mesBom)
    {
        return mesBomMapper.selectMesBomList(mesBom);
    }

    /**
     * 新增制造执行MES-BOM
     * 
     * @param mesBom 制造执行MES-BOM
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesBom(MesBomVo mesBom)
    {
        int rows = mesBomMapper.insertMesBom(mesBom);
        insertMesBomDetailItem(mesBom);
        return rows;
    }

    /**
     * 修改制造执行MES-BOM
     * 
     * @param mesBom 制造执行MES-BOM
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesBom(MesBomVo mesBom)
    {
        mesBomMapper.deleteMesBomDetailItemByBomId(mesBom.getId());
        insertMesBomDetailItem(mesBom);
        return mesBomMapper.updateMesBom(mesBom);
    }

    /**
     * 批量删除制造执行MES-BOM
     * 
     * @param ids 需要删除的制造执行MES-BOM主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesBomByIds(Long[] ids)
    {
        mesBomMapper.deleteMesBomDetailItemByBomIds(ids);
        return mesBomMapper.deleteMesBomByIds(ids);
    }

    /**
     * 删除制造执行MES-BOM信息
     * 
     * @param id 制造执行MES-BOM主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesBomById(Long id)
    {
        mesBomMapper.deleteMesBomDetailItemByBomId(id);
        return mesBomMapper.deleteMesBomById(id);
    }

    /**
     * 新增制造执行MES-BOM明细信息信息
     * 
     * @param mesBom 制造执行MES-BOM对象
     */
    public void insertMesBomDetailItem(MesBomVo mesBom)
    {
        List<MesBomDetailItem> mesBomDetailItemList = mesBom.getMesBomDetailItemList();
        Long id = mesBom.getId();
        if (StringUtils.isNotNull(mesBomDetailItemList))
        {
            List<MesBomDetailItem> list = new ArrayList<MesBomDetailItem>();
            for (MesBomDetailItem mesBomDetailItem : mesBomDetailItemList)
            {
                mesBomDetailItem.setBomId(id);
                list.add(mesBomDetailItem);
            }
            if (list.size() > 0)
            {
                mesBomMapper.batchMesBomDetailItem(list);
            }
        }
    }
}
